package com.example.boot.controller.simple_test;

import com.example.boot.domain.Payment;
import com.example.boot.exceptionhandler.GuliException;
import com.example.boot.service.simple_test.IOrderService;
import com.example.boot.service.simple_test.IPaymentService;
import com.example.boot.service.simple_test.impl.PaymentServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class PaymentController {

    @Resource
    private IPaymentService paymentService;
    @Resource
    private IOrderService iOrderService;

    @GetMapping(value = "/query")
    public Payment testTrans(){
        paymentService.executeAsync();
        Payment payment = paymentService.getById(10001);
        System.out.println("------------main end-----------");
        return payment;
    }

    @GetMapping("/testUpdate")
    public String testUpdate(@RequestParam String str){
        paymentService.testUpdate(str);
        return "success";
    }

    @GetMapping("/testSingle")
    public String testSingle(){
        System.out.println(this);
        System.out.println(paymentService);
        System.out.println(new PaymentServiceImpl());
        return "旭辉是sb";
    }

    @GetMapping("/asyn")
    public String testAsyn(){
        Runnable rn = ()->{
            paymentService.testAsyn();
        };
        new Thread(rn).start();
        return "success";
    }

    @GetMapping("/testTransaction")
    public String testTransaction(){
        try {
            paymentService.testTransaction1();
        } catch (Exception e) {
            return e.getMessage();
        }
        return "success";
    }

    @GetMapping("/test1")
    public String test1(){
        try {
            paymentService.testTran1();
        } catch (Exception e) {
            return e.getMessage();
        }
        return "success";
    }


    @GetMapping("/testexcetion/{id}")
    public String test2(@PathVariable(value = "id") Integer id){
        if (id == 0){
            int i = 1/0;
        }if (id == 1){
            throw new GuliException(500, "自定义异常");
        }if (id == 2){
            throw new RuntimeException("RuntimeException");
        }
        return "success";
    }
}
